<?php
    $xtpl_list  = new XTemplate("view/news/list.htm");
    
    $title      = "Thông tin khuyến mại";
    $offset     = $_GET['p'];
    $limit      = 5;
    $offset     = (isset($offset) && ($offset > 0)) ? $offset : 0;
    
    $keyword    = $_POST['keyword'];
   	$keyword	= str_replace(" ", "%", $keyword);
   	$keyword 	= "LIKE '%" . $keyword . "%'";
    $condition  = "title $keyword";
    
    if(isset($_POST['submit'])){
        $totalRecords   = 0;
        if($_POST['keyword']!=NULL){
            $query          = "SELECT * FROM news WHERE $condition";
            $re             = mysql_query($query);
            if(isset($re)){
                while($row = mysql_fetch_array($re)){
                    $sqldate    = strtotime($row['date']);
                    $date       = date('d-m-Y', $sqldate);
                    
                    $xtpl_list  -> assign('title', $row['title']);
                    $xtpl_list  -> assign('id3', $row['id_n']);
                    $xtpl_list  -> assign('time', $row['time']);
                    $xtpl_list  -> assign('date', $date);
                    $xtpl_list  -> assign('id1', $row['id_n']);
                    $xtpl_list  -> assign('id2', $row['id_n']);
                    $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row));
                    $totalRecords++;
                }
            }             
        }
        else{
            $error  = "Bạn chưa điền từ khóa";
            $xtpl_list  -> assign('error', $error);
        }
    }
    else{
        $query          = "SELECT count(*) AS totalRecords FROM news";
        $re             = mysql_query($query);
        $rows           = mysql_fetch_assoc($re);
        $totalRecords   = $rows['totalRecords'];
        
        $pages  = pagging($totalRecords, $offset, $limit, '?page=news');
        $sql    = "SELECT * FROM news ORDER BY id_n DESC LIMIT " . $offset . ", " . $limit . "";
        $result = mysql_query($sql);
        while($row = mysql_fetch_array($result)){
            $sqldate    = strtotime($row['date']);
            $date       = date('d-m-Y', $sqldate);
            
            $xtpl_list  -> assign('title', $row['title']);
            $xtpl_list  -> assign('id3', $row['id_n']);
            $xtpl_list  -> assign('time', $row['time']);
            $xtpl_list  -> assign('date', $date);
            $xtpl_list  -> assign('id1', $row['id_n']);
            $xtpl_list  -> assign('id2', $row['id_n']);
            $xtpl_list  -> insert_loop('LIST.TABLES', array('LIST'=>$row));
        }
    }
    
    if(isset($_POST['add'])){
        header("location:index.php?page=news&module=add");
    }
    
    if(isset($_POST['delete'])){
        if(is_array($_POST['check'])){
            $list_id    = join($_POST['check'], ',');
            $sql    = "DELETE FROM news WHERE id_n IN (" . $list_id . ")";
            $result = mysql_query($sql);
            header("location:index.php?page=news");
        }
    }
    
    $xtpl_list  -> assign('total', $totalRecords);
    $xtpl_list   -> assign('pages', $pages);   
    $xtpl_list   -> parse("LIST");
    $content     = $xtpl_list -> text("LIST");
?>